package com.yanqu.road.server.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_ENTER_NAME_MATCH_GROUPING_NAME_SYNC_FROM_CROSS, desc = "分组匹配结果下发")
public class EnterNameGroupingFromChannelCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        EnterNameProto.CrossEnterNameGroupingSyncMsg syncMsg = EnterNameProto.CrossEnterNameGroupingSyncMsg.parseFrom(yanQuMessage.getBytes());

        List<ActivityGroupMatch> list = new ArrayList<>();
        for (EnterNameProto.ChannelEnterNameGroupingMsg groupingMsg : syncMsg.getMatchList()) {
            ActivityGroupMatch match = new ActivityGroupMatch();
            match.setActivityId(syncMsg.getActivityId());
            match.setGroupId(groupingMsg.getGroupId());
            match.setEnemyGroupId(groupingMsg.getEnemyGroupId());
            match.setZoneIdList(groupingMsg.getZoneListList());
            list.add(match);
        }
        int activityId = syncMsg.getActivityId();

        EnterNameActivityMgr.getMatchMap().put(syncMsg.getActivityId(), list);

        EnterNameActivityMgr.getLogger().info("enter name grouping activityId {}, group size {}", syncMsg.getActivityId(), list.size());
        for (EnterNameProto.UserEnterNameMsg nameMsg : syncMsg.getUserList()) {
            EnterNameActivityMgr.notifyGrouping(activityId, nameMsg, list);
        }
    }
}
